<?php


class user_handler implements handler{
	function run($task,$params=array()){
		global $user,$core,$document,$config;
		switch($task){
			case "login":
				if($user->loginWidthCredentials($_POST["user"],$_POST["password"]))
				{
					$return=$_POST["returnOK"]?$_POST["returnOK"]:"/";
				}
				else {
					$return=$_POST["returnError"]?$_POST["returnError"]:"/";
					$_SESSION["alertas"][]="Login Incorrecto";
				}
				header("location:".$return);
				exit;
				break;
			case "logout":
				$user->logout();
				header("location:/");
				exit;
				break;
			case "loginwithfacebook":
				$facebook=$core->getApp("facebook");
				if($facebook->submitCode( $_GET["code"])==USER_LOGGEDBUTNOTREGISTERED){
					if($document){
						$document->addScript("js/register.js");
						
					};
				}
				break;
			case "gettwittertoken":
				$twitter=$core->getApp("twitter");
				$ruta=$twitter->getRequestToken($config->paths["url"]."user/loginwithtwitter");
				if($ruta){
					
					header("location:".$ruta);
				}
				else {
					header("location:/");
				}
				exit;
				break;
			case "loginwithtwitter":
				$twitter=$core->getApp("twitter");
				
				$datos=$twitter->getAccessToken($_GET["oauth_token"],$_GET["oauth_verifier"]);
				if($user->loginwithtwitter($datos)==USER_LOGGEDBUTNOTREGISTERED){
					if($document){
						$document->addScript("js/register.js");
						
					};
				}
				break;
		}
	}
}